﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Diagnostics;
using SimpleIR;

namespace InterpreterCore
{
	/// <summary>
	/// This class contains a bunch of static/global methods for managing the filesystem state in addition to other external system state.
	/// </summary>
	public static class FileStateAndSystem
	{
		////////////////////////////////
		//Filesystem state

		private static Dictionary<int, System.IO.FileStream> s_filesystemmap;

		private static int s_fileidctr;

		internal static void InitializeFileSystem()
		{
			s_filesystemmap = new Dictionary<int, System.IO.FileStream>();
			s_fileidctr = 0;
		}

		internal static Dictionary<int, System.IO.FileStream> FileSystemMap
		{ get { return s_filesystemmap; } }

		internal static int OpenFileAddDesciptor(string filename, System.IO.FileMode fm, System.IO.FileAccess fa, System.IO.FileShare fs)
		{
			System.IO.FileStream fsval = new System.IO.FileStream(filename, fm, fa, fs);
			int fsid = s_fileidctr++;
			s_filesystemmap.Add(fsid, fsval);

			return fsid;
		}

		internal static void CloseFileDescriptor(int descriptor)
		{
			if(s_filesystemmap.ContainsKey(descriptor))
			{
				s_filesystemmap[descriptor].Close();
				s_filesystemmap.Remove(descriptor);
			}
		}
	}
}